#!/usr/bin/make -f

# Find the arch we are building for, as this determines
# the location of plugins in /usr/lib
SRC_DIR = /usr/src/netdata
TOP = $(CURDIR)/debian/netdata
TEMPTOP = $(CURDIR)/debian/tmp
BUILDDIR = $(CURDIR)/debian/build

BASE_CONFIG = system/netdata.conf

SYSTEMD_VERSION = $(shell /bin/sh -c "systemd --version 2>&1 | head -n 1 | cut -f 2 -d ' '")

ifeq ($(shell test $(SYSTEMD_VERSION) -gt 235 && echo "1"), 1)
SYSTEMD_UNIT = $(BUILDDIR)/system/systemd/netdata.service
else
SYSTEMD_UNIT = $(BUILDDIR)/system/systemd/netdata.service.v235
endif

ifeq ($(shell test ${DEB_TARGET_ARCH} != "amd64" && echo "1"), 1)
HAVE_EBPF = 0
EBPF_CONFIG = -DENABLE_PLUGIN_EBPF=Off
else
HAVE_EBPF = 1
EBPF_CONFIG = -DENABLE_PLUGIN_EBPF=On
endif

ifeq ($(shell test ${DEB_TARGET_ARCH} != "amd64" && echo "1"), 1)
ifeq ($(shell test ${DEB_TARGET_ARCH} != "arm64" && echo "1"), 1)
HAVE_XENSTAT = 0
XENSTAT_CONFIG = -DENABLE_PLUGIN_XENSTAT=Off
else
HAVE_XENSTAT = 1
XENSTAT_CONFIG = -DENABLE_PLUGIN_XENSTAT=On
endif
else
HAVE_XENSTAT = 1
XENSTAT_CONFIG = -DENABLE_PLUGIN_XENSTAT=On
endif

ifeq ($(ENABLE_SENTRY),true)
    RELEASE_PIPELINE ?= Unknown
    VERSION ?= Unknown
    BUILD_DESTINATION ?= Unknown
    SENTRY_CONFIG := -DENABLE_SENTRY=On \
        -DNETDATA_SENTRY_ENVIRONMENT=$(RELEASE_PIPELINE) \
        -DNETDATA_SENTRY_RELEASE=$(VERSION) \
        -DNETDATA_SENTRY_DIST=$(BUILD_DESTINATION) \
        -DNETDATA_SENTRY_DSN=$(SENTRY_DSN)
else
    SENTRY_CONFIG := -DENABLE_SENTRY=Off
endif


%:
	dh $@ --builddirectory=$(BUILDDIR)

override_dh_installinit:
	echo "SystemD Version: $(SYSTEMD_VERSION)"
	echo "Using SystemD Unit: $(SYSTEMD_UNIT)"
	cp -v $(SYSTEMD_UNIT) debian/netdata.service

	dh_systemd_enable
	dh_installinit

override_dh_auto_configure:
	if [ $(HAVE_EBPF) -eq 1 ]; then \
		packaging/bundle-libbpf.sh . ${TOP}/usr/libexec/netdata/plugins.d; \
		packaging/bundle-ebpf-co-re.sh . ${TOP}/usr/libexec/netdata/plugins.d; \
	fi
	dh_auto_configure -- -G Ninja \
		-DCMAKE_BUILD_TYPE=RelWithDebInfo \
		-DCMAKE_INSTALL_PREFIX=/ \
		-DWEB_DIR=/var/lib/netdata/www \
		-DCMAKE_C_FLAGS='-ffile-prefix-map=${SRC_DIR}=${SRC_DIR}' \
		-DENABLE_ACLK=On \
		-DENABLE_CLOUD=On \
		-DENABLE_DBENGINE=On \
		-DENABLE_H2O=On \
		-DENABLE_ML=On \
		-DENABLE_PLUGIN_APPS=On \
		-DENABLE_PLUGIN_CGROUP_NETWORK=On \
		-DENABLE_PLUGIN_DEBUGFS=On \
		$(EBPF_CONFIG) \
		-DENABLE_PLUGIN_FREEIPMI=On \
		-DENABLE_PLUGIN_GO=On \
		-DENABLE_PLUGIN_LOCAL_LISTENERS=On \
		-DENABLE_PLUGIN_LOGS_MANAGEMENT=On \
		-DENABLE_PLUGIN_NFACCT=On \
		-DENABLE_PLUGIN_PERF=On \
		-DENABLE_PLUGIN_SLABINFO=On \
		-DENABLE_PLUGIN_SYSTEMD_JOURNAL=On \
		$(XENSTAT_CONFIG) \
		-DENABLE_EXPORTER_PROMETHEUS_REMOTE_WRITE=On \
		-DENABLE_EXPORTER_MONGODB=On \
		-DENABLE_BUNDLED_PROTOBUF=Off \
		-DENABLE_BUNDLED_JSONC=Off \
		${SENTRY_CONFIG} \
		-DENABLE_BUNDLED_YAML=Off

override_dh_auto_test:
	true

override_dh_strip:
	if [ "${ENABLE_SENTRY}" = "true" ] && [ "${UPLOAD_SENTRY}" = "true" ]; then \
		sentry-cli debug-files upload -o netdata-inc -p netdata-agent --force-foreground --log-level=debug --wait --include-sources /usr/src/netdata/debian/netdata/usr/sbin/netdata; \
	fi
	dh_strip

override_dh_install:
	cp -v $(BASE_CONFIG) debian/netdata.conf

	dh_install

	# Set the CUPS plugin install rule
	#
	mkdir -p $(TOP)-plugin-cups/usr/libexec/netdata/plugins.d
	mv -f $(TEMPTOP)/usr/libexec/netdata/plugins.d/cups.plugin \
	$(TOP)-plugin-cups/usr/libexec/netdata/plugins.d/cups.plugin

	# Add free IPMI plugin install rules
	#
	mkdir -p $(TOP)-plugin-freeipmi/usr/libexec/netdata/plugins.d
	mv -f $(TEMPTOP)/usr/libexec/netdata/plugins.d/freeipmi.plugin \
	$(TOP)-plugin-freeipmi/usr/libexec/netdata/plugins.d/freeipmi.plugin

	# Add NFACCT plugin install rules
	#
	mkdir -p $(TOP)-plugin-nfacct/usr/libexec/netdata/plugins.d
	mv -f $(TEMPTOP)/usr/libexec/netdata/plugins.d/nfacct.plugin \
	$(TOP)-plugin-nfacct/usr/libexec/netdata/plugins.d/nfacct.plugin

	# Add charts.d plugin install rules
	#
	mkdir -p $(TOP)-plugin-chartsd/usr/libexec/netdata/plugins.d/
	mkdir -p $(TOP)-plugin-chartsd/usr/lib/netdata/conf.d/
	mv -f $(TEMPTOP)/usr/libexec/netdata/plugins.d/charts.d.plugin \
	$(TOP)-plugin-chartsd/usr/libexec/netdata/plugins.d/charts.d.plugin
	mv -f $(TEMPTOP)/usr/libexec/netdata/plugins.d/charts.d.dryrun-helper.sh \
	$(TOP)-plugin-chartsd/usr/libexec/netdata/plugins.d/charts.d.dryrun-helper.sh
	mv -f $(TEMPTOP)/usr/libexec/netdata/charts.d \
	$(TOP)-plugin-chartsd/usr/libexec/netdata/charts.d
	mv -f $(TEMPTOP)/usr/lib/netdata/conf.d/charts.d.conf \
	$(TOP)-plugin-chartsd/usr/lib/netdata/conf.d/charts.d.conf
	mv -f $(TEMPTOP)/usr/lib/netdata/conf.d/charts.d \
	$(TOP)-plugin-chartsd/usr/lib/netdata/conf.d/charts.d

	# Add ebpf plugin install rules
	if [ $(HAVE_EBPF) -eq 1 ]; then \
		mkdir -p $(TOP)-plugin-ebpf/usr/libexec/netdata/plugins.d/; \
		mkdir -p $(TOP)-plugin-ebpf/usr/lib/netdata/conf.d/; \
		mv -f $(TEMPTOP)/usr/libexec/netdata/plugins.d/ebpf.plugin $(TOP)-plugin-ebpf/usr/libexec/netdata/plugins.d/ebpf.plugin; \
		mv -f $(TEMPTOP)/usr/lib/netdata/conf.d/ebpf.d.conf $(TOP)-plugin-ebpf/usr/lib/netdata/conf.d/ebpf.d.conf; \
		mv -f $(TEMPTOP)/usr/lib/netdata/conf.d/ebpf.d $(TOP)-plugin-ebpf/usr/lib/netdata/conf.d/ebpf.d; \
	fi

	# Add python plugin install rules
	mkdir -p $(TOP)-plugin-pythond/usr/libexec/netdata/plugins.d/
	mkdir -p $(TOP)-plugin-pythond/usr/lib/netdata/conf.d/
	mv -f $(TEMPTOP)/usr/libexec/netdata/plugins.d/python.d.plugin \
	$(TOP)-plugin-pythond/usr/libexec/netdata/plugins.d/python.d.plugin
	mv -f $(TEMPTOP)/usr/libexec/netdata/python.d \
	$(TOP)-plugin-pythond/usr/libexec/netdata/python.d
	mv -f $(TEMPTOP)/usr/lib/netdata/conf.d/python.d.conf \
	$(TOP)-plugin-pythond/usr/lib/netdata/conf.d/python.d.conf
	mv -f $(TEMPTOP)/usr/lib/netdata/conf.d/python.d \
	$(TOP)-plugin-pythond/usr/lib/netdata/conf.d/python.d

	# Add apps plugin install rules
	mkdir -p $(TOP)-plugin-apps/usr/libexec/netdata/plugins.d/
	mkdir -p $(TOP)-plugin-apps/usr/lib/netdata/conf.d/
	mv -f $(TEMPTOP)/usr/libexec/netdata/plugins.d/apps.plugin \
	$(TOP)-plugin-apps/usr/libexec/netdata/plugins.d/apps.plugin
	mv -f $(TEMPTOP)/usr/lib/netdata/conf.d/apps_groups.conf \
	$(TOP)-plugin-apps/usr/lib/netdata/conf.d/apps_groups.conf

	# Add slabinfo plugin install rules
	mkdir -p $(TOP)-plugin-slabinfo/usr/libexec/netdata/plugins.d/
	mv -f $(TEMPTOP)/usr/libexec/netdata/plugins.d/slabinfo.plugin \
	$(TOP)-plugin-slabinfo/usr/libexec/netdata/plugins.d/slabinfo.plugin

	# Add perf plugin install rules
	mkdir -p $(TOP)-plugin-perf/usr/libexec/netdata/plugins.d/
	mv -f $(TEMPTOP)/usr/libexec/netdata/plugins.d/perf.plugin \
	$(TOP)-plugin-perf/usr/libexec/netdata/plugins.d/perf.plugin

	# Add debugfs plugin install rules
	mkdir -p $(TOP)-plugin-debugfs/usr/libexec/netdata/plugins.d/
	mv -f $(TEMPTOP)/usr/libexec/netdata/plugins.d/debugfs.plugin \
	$(TOP)-plugin-debugfs/usr/libexec/netdata/plugins.d/debugfs.plugin

	# Add systemd-journal plugin install rules
	mkdir -p $(TOP)-plugin-systemd-journal/usr/libexec/netdata/plugins.d/
	mv -f $(TEMPTOP)/usr/libexec/netdata/plugins.d/systemd-journal.plugin \
	$(TOP)-plugin-systemd-journal/usr/libexec/netdata/plugins.d/systemd-journal.plugin

	# Add logs-management plugin install rules
	mkdir -p $(TOP)-plugin-logs-management/usr/libexec/netdata/plugins.d/
	mv -f $(TEMPTOP)/usr/libexec/netdata/plugins.d/logs-management.plugin \
	$(TOP)-plugin-logs-management/usr/libexec/netdata/plugins.d/logs-management.plugin
	mkdir -p $(TOP)-plugin-logs-management/usr/lib/netdata/conf.d/
	mv -f $(TEMPTOP)/usr/lib/netdata/conf.d/logsmanagement.d.conf \
	$(TOP)-plugin-logs-management/usr/lib/netdata/conf.d/logsmanagement.d.conf
	mv -f $(TEMPTOP)/usr/lib/netdata/conf.d/logsmanagement.d/ \
	$(TOP)-plugin-logs-management/usr/lib/netdata/conf.d/logsmanagement.d/

	# Add xenstat plugin install rules
	if [ $(HAVE_XENSTAT) -eq 1 ]; then \
		mkdir -p $(TOP)-plugin-xenstat/usr/libexec/netdata/plugins.d/ ; \
		mv -f $(TEMPTOP)/usr/libexec/netdata/plugins.d/xenstat.plugin \
		$(TOP)-plugin-xenstat/usr/libexec/netdata/plugins.d/xenstat.plugin; \
	fi

	# Install go to it's own package directory
	#
	mkdir -p $(TOP)-plugin-go/usr/libexec/netdata/plugins.d
	mv -f $(TEMPTOP)/usr/libexec/netdata/plugins.d/go.d.plugin \
	$(TOP)-plugin-go/usr/libexec/netdata/plugins.d/go.d.plugin
	mkdir -p $(TOP)-plugin-go/usr/lib/netdata/conf.d
	mv -f $(TEMPTOP)/usr/lib/netdata/conf.d/go.d.conf \
	$(TOP)-plugin-go/usr/lib/netdata/conf.d/go.d.conf
	mv -f $(TEMPTOP)/usr/lib/netdata/conf.d/go.d \
	$(TOP)-plugin-go/usr/lib/netdata/conf.d/go.d

	# Add network-viewer plugin install rules
	mkdir -p $(TOP)-plugin-network-viewer/usr/libexec/netdata/plugins.d/
	mv -f $(TEMPTOP)/usr/libexec/netdata/plugins.d/network-viewer.plugin \
	$(TOP)-plugin-network-viewer/usr/libexec/netdata/plugins.d/network-viewer.plugin

	# Set the rest of the software in the main package
	#
	cp -rp $(TEMPTOP)/usr $(TOP)
	cp -rp $(TEMPTOP)/var $(TOP)
	cp -rp $(TEMPTOP)/etc $(TOP)
	mkdir -p "$(TOP)/var/log/netdata"
	mkdir -p "$(TOP)/var/cache/netdata"
	mkdir -p "$(TOP)/var/run/netdata"

	# Copy the updater script
	#
	cp -v packaging/installer/netdata-updater.sh $(TOP)/usr/libexec/netdata/netdata-updater.sh

	# Handle eBPF code
	#
	if [ $(HAVE_EBPF) -eq 1 ]; then \
		mkdir -p $(TOP)-ebpf-code-legacy/usr/libexec/netdata/plugins.d/; \
		packaging/bundle-ebpf.sh . ${TOP}-ebpf-code-legacy/usr/libexec/netdata/plugins.d/ force; \
	fi


override_dh_installdocs:
	dh_installdocs

	find . \
		-name README.md \
		-not -path './debian/*' \
		-not -path './contrib/*' \
		-exec cp \
		--parents \
		--target $(TOP)/usr/share/doc/netdata/ \
		{} \;

override_dh_fixperms:
	dh_fixperms

	# Updater script should be executable
	#
	chmod 0755 $(TOP)/usr/libexec/netdata/netdata-updater.sh

	# debugfs plugin
	chmod 0750 $(TOP)-plugin-debugfs/usr/libexec/netdata/plugins.d/debugfs.plugin

	# apps.plugin should only be runnable by the netdata user. It will be
	# given extra capabilities in the postinst script.
	chmod 0750 $(TOP)-plugin-apps/usr/libexec/netdata/plugins.d/apps.plugin

	# slabinfo package
	chmod 0750 $(TOP)-plugin-slabinfo/usr/libexec/netdata/plugins.d/slabinfo.plugin

	# perf package
	chmod 0750 $(TOP)-plugin-perf/usr/libexec/netdata/plugins.d/perf.plugin

	# Go plugin package
	chmod 0750 $(TOP)-plugin-go/usr/libexec/netdata/plugins.d/go.d.plugin

	# CUPS plugin package
	chmod 0750 $(TOP)-plugin-cups/usr/libexec/netdata/plugins.d/cups.plugin

	# freeIPMI plugin package
	chmod 4750 $(TOP)-plugin-freeipmi/usr/libexec/netdata/plugins.d/freeipmi.plugin

	# NFACCT plugin package
	chmod 4750 $(TOP)-plugin-nfacct/usr/libexec/netdata/plugins.d/nfacct.plugin

	# cgroup-network
	chmod 4750 $(TOP)/usr/libexec/netdata/plugins.d/cgroup-network

	# local-listeners
	chmod 4750 $(TOP)/usr/libexec/netdata/plugins.d/local-listeners

	# network-viewer
	chmod 4750 $(TOP)-plugin-network-viewer/usr/libexec/netdata/plugins.d/network-viewer.plugin

	# systemd-journal
	chmod 4750 $(TOP)-plugin-systemd-journal/usr/libexec/netdata/plugins.d/systemd-journal.plugin

	# xenstat
	if [ $(HAVE_XENSTAT) -eq 1 ]; then \
		chmod 4750 $(TOP)-plugin-xenstat/usr/libexec/netdata/plugins.d/xenstat.plugin; \
	fi

	# logs-management
	chmod 4750 $(TOP)-plugin-logs-management/usr/libexec/netdata/plugins.d/logs-management.plugin

override_dh_installlogrotate:
	cp $(BUILDDIR)/system/logrotate/netdata debian/netdata.logrotate
	dh_installlogrotate

override_dh_installdeb:
	dh_installdeb
	@echo "Recreating conffiles without auto-adding /etc files"
	@cp $(CURDIR)/debian/conffiles $(CURDIR)/debian/netdata/DEBIAN/conffiles

override_dh_clean:
	dh_clean

	# Tidy up copied/generated files
	#
	[ -r $(CURDIR)/debian/netdata.logrotate ] && rm $(CURDIR)/debian/netdata.logrotate ; true
	[ -r $(CURDIR)/debian/netdata.conffiles ] && rm $(CURDIR)/debian/netdata.conffiles ; true
